# -*- coding: utf-8 -*-

# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: http://doc.scrapy.org/en/latest/topics/item-pipeline.html

import MySQLdb


class AboutZhihuUserSpiderPipeline(object):
    def process_item(self, item, spider):
        return item


class FileStorePipeline(object):
    def __init__(self):
        self.f = open("f://n1trox.txt", 'wb')

    def process_item(self, item, spider):
        self.f.write(item["aboutUrl"])
        self.f.write('\r\n')

        return item


class MySQLStorePipeline(object):
    '''
    fix scrapy in linux
    error: command 'gcc' failed with exit status 1 的解决办法
    用pip安装软件时提示：error: command 'gcc' failed with exit status 1
    easy_install也提示一样的错误：error: Setup script exited with error: command 'gcc' failed with exit status 1
    一般是缺少python-dev包
    apt-get install python-dev或yum install python-devel这样就行了
    fix charset in simple chinese
    ALTER DATABASE databasename CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    ALTER TABLE tablename CONVERT TO CHARACTER SET utf8 COLLATE utf8_unicode_ci;
    '''

    def __init__(self):
        self.conn = MySQLdb.connect(user="root",
                                    passwd="toor123",
                                    db="zhihu",
                                    host="115.29.148.86",
                                    charset="utf8",
                                    use_unicode=True)
        self.cursor = self.conn.cursor()

    def process_item(self, item, spider):
        print "start insert data"
        try:
            self.cursor.execute("""insert into abouturls values(%s)""",
                                (item["aboutUrl"]))

            self.conn.commit()
        except MySQLdb.Error, e:
            print "Error %d: %s" % (e.args[0], e.args[1])
            return item
